package com.vf.editor.mvc.scene.onlineMap;

import java.util.List;

import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.vf.editor.common.controller.BaseController;
import com.vf.editor.common.model.resources.SceneLayer;
import com.vf.editor.common.model.resources.ServerMap;
import com.vf.editor.common.util.UuidUtil;
import com.vf.editor.mvc.scene.layer.SceneLayerService;

@Path(value = "/scene/onlineMap", viewPath = "/views/scene/onlineMap/")
public class OnlineMapController extends BaseController{

	@Inject
	private SceneLayerService sceneLayerService;
	
	public void library() {
		this.render("library.html");
	}
	
	public void listData() {
		String sceneId=this.getPara("sceneId");
		List<ServerMap> list = ServerMap.dao.find("select a.*,(CASE  WHEN (SELECT COUNT(ID) AS COUNT FROM "+SceneLayer.TABLE_NAME+" WHERE sceneId=? and a.id=thingId )>0 THEN TRUE ELSE  FALSE END ) AS checked from "+ServerMap.TABLE_NAME+" a where classify=?",sceneId,this.getPara("classify"));
		renderPage(0, list, "", list.size());
	}
	
	public void add() {
		String sceneId=this.getPara("sceneId");
		String thingId=this.getPara("id");
		ServerMap serverMap = ServerMap.dao.findById(getPara("id"));
		if(serverMap!=null) {
			SceneLayer sceneLayer=SceneLayer.dao.findFirst("select * from "+SceneLayer.TABLE_NAME+" where sceneId=? and thingId=? ",sceneId,thingId);
			if(sceneLayer==null) {
				sceneLayer=new SceneLayer();
				sceneLayer.setId(UuidUtil.getUUID());
				sceneLayer.setName(serverMap.getName());
				sceneLayer.setCode(serverMap.getCode());
				sceneLayer.setType("0");
				sceneLayer.setSceneId(sceneId);
				sceneLayer.setParentId(sceneId);
				sceneLayer.setThingId(thingId);
				if(sceneLayer.save()) {
					serverMap.setId(sceneLayer.getId());
					this.renderSuccess("成功！", serverMap);
				}else {
					this.renderError("添加失败！");
				}
			}else {
				serverMap.setId(sceneLayer.getId());
				this.renderSuccess("成功！", serverMap);
			}
		}else {
			this.renderError("数据不存在！");
		}
	}
	
	public void findById() {
		String sceneId=this.getPara("sceneId");
		String thingId=this.getPara("id");
		SceneLayer sceneLayer=SceneLayer.dao.findFirst("select * from "+SceneLayer.TABLE_NAME+" where sceneId=? and thingId=? ",sceneId,thingId);
		if(sceneLayer!=null) {
			ServerMap serverMap = ServerMap.dao.findById(sceneLayer.getId());
			if(serverMap!=null) {
				serverMap.setId(sceneLayer.getId());
				this.renderSuccess("成功！", serverMap);
			}else {
				this.renderError("数据不存在！");
			}
		}else {
			this.renderError("数据不存在！");
		}
	}
	
	public void delete() {
		String sceneId=this.getPara("sceneId");
		String thingId=this.getPara("id");
		SceneLayer sceneLayer=SceneLayer.dao.findFirst("select * from "+SceneLayer.TABLE_NAME+" where sceneId=? and thingId=? ",sceneId,thingId);
		if(sceneLayer!=null) {
			if(sceneLayer.delete()) {
				this.renderSuccess("成功！", sceneLayer);
			}else {
				this.renderError("删除失败！");
			}
		}else {
			this.renderError("数据不存在！");
		}
	}
}
